<template>
  <el-dialog
    :title="title"
    :visible="dialogVisible"
    :close-on-click-modal="false"
    @close="clearForm"
  >
    <!--      内容框-->
    <el-form ref="formData" :model="formData" :rules="rules" label-width="120px">
      <el-form-item prop="name" label="权限名称">
        <el-input v-model="formData.name" placeholder="2-10个字" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="code" label="权限标识">
        <el-input v-model="formData.code" placeholder="2-20个字符" style="width: 80%" size="mini" />
      </el-form-item>
      <el-form-item prop="description" label="权限标识">
        <el-input
          v-model="formData.description"
          placeholder="2-100个字"
          type="textarea"
          size="mini"
          :rows="4"
          style="width: 80%"
        />
      </el-form-item>
      <el-form-item prop="enVisible" label="开启">
        <el-switch
          v-model="formData.enVisible"
          active-value="1"
          inactive-value="0"
        />
      </el-form-item>
      <!--      确定框-->
      <el-form-item>
        <el-row type="flex" justify="center">
          <el-col :span="12">
            <el-button size="mini" type="primary" @click="subFormData">确定</el-button>
            <el-button size="mini" @click="clearForm">取消</el-button>
          </el-col>
        </el-row>
      </el-form-item>
    </el-form>
  </el-dialog>
</template>

<script>
import { addPermission, getPermission, updatePermission } from '@/api/permission'

export default {
  name: 'PermissionDialog',
  props: {
    // 权限点列表
    allPermissions: {
      type: Array,
      default: () => []
    },
    dialogVisible: {
      type: Boolean,
      default: false
    },
    title: {
      type: String,
      default: '权限'
    },
    operateType: {
      type: String,
      default: ''
    },
    operateRoot: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      curPermission: [],
      formData: {
        name: '',
        code: '', // 权限点标识
        description: '',
        // 权限点类型 1为页面路由权限点, 2为按钮权限点
        type: 2,
        // 权限点父级id 页面权限点pid值为'0', 按钮权限点值为所属页面权限点的id值
        pid: '',
        // 权限点开启状态 0关闭, 1开启. (暂时无需判断, 前端逻辑上默认全都有效直接用)
        enVisible: true
      },
      rules: {
        // 校验: 权限点"名字"不能和子集们现有的权限点名字重复
        name: [
          { required: true, message: '请输入权限名称', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字', trigger: 'blur' },
          { validator: (_rule, value, callback) => {
            const isRepeat = this.allPermissions.some(item => item.name === value && value !== this.curPermission.name)
            if (isRepeat) {
              callback(new Error('权限名称不能重复'))
            } else {
              callback()
            }
          }, trigger: 'blur'
          }
        ],
        // 校验: 权限标识不能和所有人重复, 应该是唯一的标识重要, 后续前端需要用此值做判
        code: [
          { required: true, message: '请输入权限点标识', trigger: 'blur' },
          { min: 2, max: 10, message: '请输入2-10个字符', trigger: 'blur' },
          { validator: (_rule, value, callback) => {
            const isRepeat = this.allPermissions.some(item => item.code === value && value !== this.curPermission.code)
            if (isRepeat) {
              callback(new Error('权限点标识不能重复'))
            } else {
              callback()
            }
          }, trigger: 'blur'
          }
        ],
        description: [
          { required: true, message: '请输入权限点描述', trigger: 'blur' },
          { min: 2, max: 100, message: '请输入2-100个字符', trigger: 'blur' }
        ]
      }
    }
  },
  watch: {
    operateType: {
      immediate: true,
      handler(val) {
        if (val === 'editRoot') {
          this.getPermission()
        }
      }
    }
  },
  methods: {
    clearForm() {
      this.$refs.formData.resetFields()
      this.$emit('update:dialogVisible', false)
      this.$emit('update:operateType', '')
    },
    async getPermission() {
      this.curPermission = await getPermission(this.operateRoot)
      this.formData = this.curPermission
      console.log(this.curPermission)
    },
    subFormData() {
      this.$refs.formData.validate(async(valid) => {
        if (valid) {
          if (this.operateType === 'addRoot') {
            await addPermission({ ...this.formData, pid: this.operateRoot })
            this.$notify({
              title: '添加权限点成功',
              message: this.formData.name + ' 权限点添加成功',
              type: 'success'
            })
            this.clearForm()
            this.$emit('updatePage')
          } else {
            await updatePermission(this.formData)
            this.$notify({
              title: '修改权限点信息成功',
              message: this.formData.name + ' 权限点信息修改成功',
              type: 'success'
            })
            this.$router.go(0)
          }
        } else {
          this.$notify({
            title: '权限点操作失败',
            message: this.formData.name + ' 权限点操作失败',
            type: 'error'
          })
        }
      })
    }
  }
}
</script>

<style scoped lang="scss">

</style>
